/*
 * Copyright (C) 2008 The Android Open Source Project
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *  * Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *  * Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in
 *    the documentation and/or other materials provided with the
 *    distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

/** @defgroup if If
* @ingroup libc
*/

#ifndef _NET_IF_H_
#define _NET_IF_H_

#include <sys/socket.h>
#ifndef __LITEOS__
#include <linux/if.h>
#else
#include <liteos/if.h>
#endif
#include <sys/cdefs.h>

#ifndef IF_NAMESIZE
#define IF_NAMESIZE IFNAMSIZ
#endif

#ifdef __cplusplus
#if __cplusplus
extern "C" {
#endif /* __cplusplus */
#endif /* __cplusplus */

struct if_nameindex {
  unsigned if_index;
  char* if_name;
};


/*
Func Name:  if_indextoname
*/

/**
* @ingroup  if
*
* @par Description:
* This  function is used to convert the name of the network interface corresponding
* to the interface index ifindex. The name is placed in the buffer pointed to by ifname.
* The buffer must allow for the storage of at least IF_NAMESIZE bytes.
*
* @param    #ifindex    [IN]   The index of the network interface.
* @param    #ifname     [OUT]  The name of the network interface.
*
* @attention
* <ul>
* <li>None.</li>
* </ul>
*
* @retval    ifname    On success, the ifname was successfully converted
* @retval    NULL      On failure, NULL is returned and errno is set appropriately
*
* @par Errors
* <ul>
* <li><b>ENXIO</b>: There is no name as per the index specified.</li>
* <li><b>EINVAL</b>: The ifname is a null pointer.</li>
* </ul>
*
* @par Dependency:
* <ul><li>if.h</li></ul>
*
* @see  if_nametoindex
*
* @since Huawei LiteOS V100R001C00
*/
char *if_indextoname(unsigned int ifindex, char *ifname);

/*
Func Name:  if_nametoindex
*/

/**
* @ingroup  if
*
* @par Description:
* This function is used to convert the index of the network interface corresponding to the name ifname.
*
* @param    ifname     [IN]  The name of the network interface.
*
* @attention
* <ul>
* <li>None.</li>
* </ul>
*
* @retval    #>0       On success, the index number of the network interface was successfully converted
* @retval    #0        On failure, 0 is returned and errno is set appropriately
*
* @par Errors
* <ul>
* <li><b>ENXIO</b>: Could not find the index corresponding to the interface name.</li>
* </ul>
*
* @par Dependency:
* <ul><li>if.h</li></ul>
*
* @see  if_indextoname
*
* @since Huawei LiteOS V100R001C00
*/
unsigned int if_nametoindex(const char *ifname);

/*
Func Name:  if_nameindex
*/

/**
* @ingroup  if
*
* @par Description:
* This function returns an array of if_nameindex structures, one structure per interface.
* The if_nameindex structure holds the information about a single interface and is defined as a result of including the <if.h> header.
* The end of the array of structures is indicated by a structure with an if_index of 0 and an if_name of NULL.
* The function returns a NULL pointer upon an error, and would set errno to the appropriate value.
* The memory used for this array of structures along with the interface names pointed to by the if_name members is obtained dynamically.
*
* @param    void     [IN]  The function does not take any input instead returns all the interfaces in format if_nameindex structures.
*
* @attention
* <ul>
* <li>This API is only supported since lwip 2.0.</li>
* <li>The end of the array of structures is indicated by a structure with   an if_index of 0 and an if_name of NULL.
* The function returns a NULL   pointer upon an error, and would set errno to the appropriate value.</li>
* </ul>
*
* @retval    #if_nameindex*   On success, the function returns an array of if_nameindex structures, one structure per interface. On failure, NULL shall be returned.
*
* @par Errors
* <ul>
* <li><b>ENOBUFS</b>: Insufficient resources available.</li>
* </ul>
*
* @par Dependency:
* <ul><li>if.h</li></ul>
*
* @see  if_freenameindex
*
* @since Huawei LiteOS V100R001C00
*/
struct if_nameindex* if_nameindex(void);

/*
Func Name:  if_freenameindex
*/

/**
* @ingroup  if
*
* @par Description:
* This function frees the dynamic memory that was allocated by if_nameindex().
* The argument to this function must be a pointer that was returned by if_nameindex().
*
* @param    ptr     [IN]  The argument to this function must be a pointer that was returned by if_nameindex().
*
* @attention
* <ul>
* <li>This API is only supported since lwip 2.0.</li>
* </ul>
*
* @retval  #void    None.
*
* @par Dependency:
* <ul><li>if.h</li></ul>
*
* @see  if_nameindex
*
* @since Huawei LiteOS V100R001C00
*/
void if_freenameindex(struct if_nameindex *ptr);

#ifdef __cplusplus
#if __cplusplus
}
#endif /* __cplusplus */
#endif /* __cplusplus */

#endif